package com.iuie.ns.datum.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iuie.ns.datum.service.IBlogService;
import com.iuie.ns.datum.service.IFileInfoService;
import com.iuie.ns.datum.vo.BlogInfoVO;
import com.iuie.ns.frame.utils.StringUtils;
import com.iuie.ns.frame.web.response.vo.ResponseInfo;
import com.iuie.ns.system.service.code.ISysCodeInfoService;
import com.iuie.ns.system.vo.code.SysCodeInfoVO;

/**
 * @desc 博客模块控制器
 * @createTime 2017年8月19日 下午3:55:16
 * @author iuie
 */
@RequestMapping("datum/blog")
@Controller
public class BlogController {
	
	private static final Logger logger = LoggerFactory.getLogger(BlogController.class);
	
	@Autowired
	private ISysCodeInfoService sysCodeInfoService;
	
	@Autowired
	private IBlogService blogService;
	
	@Autowired
	private IFileInfoService fileInfoService;

	/**
	 * 列表结构博客分类--博客页面
	 * @param request
	 * @return
	 * @date 2017年8月22日 上午8:43:57
	 */
	@RequestMapping
	public String index(HttpServletRequest request){
		List<SysCodeInfoVO> blogTypes = sysCodeInfoService.findByCodeTypeId("blogType", 1);
		request.setAttribute("blogTypes", blogTypes); // 查询出blogTypes
		return "view-datum/blog/blog";
	}
	
	/**
	 * 树形结构博客分类-博客页面
	 * @param request
	 * @return
	 * @date 2017年8月22日 上午8:45:04
	 */
	@RequestMapping("treeBlog/index")
	public String blogTreeIndex(HttpServletRequest request) {
		List<SysCodeInfoVO> blogTypes = sysCodeInfoService.findByCodeTypeId("blogType", 1);
		request.setAttribute("blogTypes", blogTypes); // 查询出blogTypes
		return "view-datum/blog/treeBlog";
	}
	
	@RequestMapping("blogAdd/index/{blogType}")
	public String blogAddIndex(@PathVariable("blogType") String blogType, HttpServletRequest request) {
		request.setAttribute("blogType", blogType);
		return "view-datum/blog/blogAdd";
	}
	
	@RequestMapping("blogUpdate/index/{blogId}")
	public String blogUpdateIndex(@PathVariable("blogId") String blogId, HttpServletRequest request) {
		request.setAttribute("blogId", blogId);
		return "view-datum/blog/blogUpdate";
	}
	
	@RequestMapping("editBlogType/index")
	public String blogTypeEditIndex() {
	    return "view-datum/blog/editBlogType";
	}
	
	@RequestMapping("editTreeBlogType/index")
	public String treeBlogTypeEditIndex() {
	    return "view-datum/blog/editTreeBlogType";
	}
	
	/**
	 * 根据blog type查询博客
	 * @param type 博客类型
	 * @return
	 */
	@RequestMapping(value="article/{type}")
	@ResponseBody
	public ResponseInfo findBlogByBlogType(@PathVariable("type") String type){
		if (StringUtils.isBlank(type)) {
			return ResponseInfo.errorInfo("错误：查询参数为空!");
		}
		List<BlogInfoVO> list = blogService.findByBlogType(type);
		return ResponseInfo.successInfo(list);
	}
	
	/**
	 * 根据blog id 查询博客
	 * @param blogId 博客id
	 * @return
	 */
	@RequestMapping(value="findById/{blogId}")
	@ResponseBody
	public ResponseInfo findById(@PathVariable("blogId") String blogId){
		if (StringUtils.isBlank(blogId)) {
			return ResponseInfo.errorInfo("查询参数为空");
		}
		Long id = Long.valueOf(blogId);
		BlogInfoVO blogInfo = blogService.findById(id);
		return ResponseInfo.successInfo(blogInfo);
	}
	
	/**
	 * 根据blog Id删除博客
	 */
	@RequestMapping(value="delete/{blogId}")
	@ResponseBody
	public ResponseInfo deleteBlog(@PathVariable("blogId") Long blogId){
		blogService.deleteById(blogId);
		return ResponseInfo.successInfo("博客删除成功！");
	}
	
	/**
	 * 标记阅读
	 * @param blogId
	 * @return
	 */
	@RequestMapping(value="signRead/{blogId}")
	@ResponseBody
	public ResponseInfo signRead(@PathVariable("blogId") Long blogId){
		blogService.updateBySignRead(blogId);
		return ResponseInfo.successInfo(blogId);
	}
	
	/**
	 * 新增博客
	 * @param blogInfo
	 * @return
	 * @throws IOException
	 * @date 2017年8月30日 上午9:51:23
	 */
	@RequestMapping(value="addBlog")
	@ResponseBody
	public ResponseInfo addOne(BlogInfoVO blogInfo) throws IOException{
		BlogInfoVO mInfo = blogService.addBlog(blogInfo);
		logger.info("新增博客成功：{}",mInfo);
		return ResponseInfo.successInfo("博客保存成功！");
	}
	
}
